var config = require('../../utils/config');
var url = config.url;
var util = require('../../utils/util');
var db = require('../../utils/db');
var openid = wx.getStorageSync('openid');//暂时hard code，应该是从登陆用户唯一标识

Page({
  data: {
    bookList: []
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.queryAllBorrowBooks();
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  formSubmit:function(e){
    console.log(e.detail)
    console.log(this.data)
    var formdata = e.detail.value
    formdata.isbn = this.data.bookMsg.isbn
    formdata.openid = wx.getStorageSync('openid')
    console.log(111111,formdata,111111111)  

    // 发送数据到 web服务器,完成借阅
    util.request({
      url:'http://192.168.111.139:5000/api/book/borrow',
      data:formdata,
      method:'POST'
    },function(res){
    
      // 判断返回的结果
      if(res.data.error == 0){
        wx.showToast({
          title:res.data.msg,
          icon: 'success',
          duration: 2000,
          complete:function(){
            // 跳转到已借阅的页面
            wx.navigateTo({
              url: '../bookList/bookList'
            });
          }
        })
      
      }else{
        wx.showToast({
          title:res.data.msg,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  backBook: function (e){
  var that = this;
  var borrowid = e.currentTarget.dataset.borrowid;

    // 发送数据到 web服务器,完成借阅
    util.request({
      url: 'http://192.168.111.139:5000/api/book/haishu',
      data: {'borrowid':borrowid},
      method: 'GET'
    }, function (res) {
      wx.navigateTo({
        url: '../bookList/bookList'
      });      
    })
  },
  queryAllBorrowBooks: function () {
    var that = this;
    var options = {
      url: config.url+'get',
      data: {
        appkey: config.appKey,
        key: openid,
        type: 'bookBorrow'
      }
    };

    util.request(options, function (res) {
      if (typeof (res.data.result) !== 'undefined') {
        var books = [];
        var results = res.data.result;
   
        for (let i = 0; i < results.length; i++) {
          books.push(results[i]);
          that.setData({
              bookList: books
          })

          // shouldBackDate
          // var option1 = {
          //   url: config.clubApi.get,
          //   data: {
          //     appkey: config.appKey,
          //     type: 'bookLibrary',
          //     key: results[i].isbn
          //   }
          // };

          // util.request(option1, (res, err) => {
          //   var book = res.data.result.value;
          //   // console.log(results[i]);
          //   // var newDate = new Date();
          //   // newDate.setTime(results[i].borrowDate);
          //   //console.log(newDate.toLocaleString());
          //   //book["borrowDate"] = newDate.toLocaleDateString();
          //   book["borrowDate"] = util.formatTime(results[i].borrowDate);
          //   book["shouldBackDate"] = util.formatTime(results[i].borrowDate+86400000*30);
          //   books.push(book);
          //   that.setData({
          //     bookList: books
          //   })
          // });

          // if (i == results.length) {
          //   console.log(books);
          //   that.setData({
          //     bookList: books
          //   })
          // }
        }


      }

    });
  },
  queryOneBook: function (key) {
    var that = this;

    var inputMsg = that.data.inputValue;
    var options = {
      url: config.clubApi.list,
      data: {
        appkey: config.appKey,
        type: 'bookLibrary',
        key: key
      }
    };

    util.request(options, (res, err) => {
      var books = [];
      for (var i = 0; i < res.data.result.length; i++) {
        books.push(res.data.result[i].value);
      }
      that.setData({
        bookList: books
      });
    });

  }
})